package com.shangxia.monomer.common.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.common.util.file
 * @date 2021/7/21 16:17
 */
public class FileUtil {
    public static void main(String[] args) throws IOException {
        String path = "F:\\bug";
        String pathName = "F:\\bug\\new 1.txt";
        List<String> files1 = readLines(pathName);
        for (String file : files1) {
            System.out.println(file);
        }

    }
    /**
     * 列出目录
     *
     * @param path
     * @return
     * @Description
     */
    public static File[] listDirectories(String path) {
        return new File(path).listFiles(File::isDirectory);
    }

    /**
     * 列出目录中的文件
     *
     * @param folder
     * @return
     */
    public static File[] listFilesInDirectory(final File folder) {
        return folder.listFiles(File::isFile);
    }

    /**
     * 递归列出目录中的文件
     *
     * @param path
     * @return
     */
    public static List<File> listAllFiles(String path) {
        List all = new ArrayList<File>();
        File[] files = new File(path).listFiles();

        if (files != null) {  // In case of access error, list is null
            for (File f : files) {
                if (f.isDirectory()) {
                    all.addAll(listAllFiles(f.getAbsolutePath()));
                } else {
                    all.add(f.getAbsoluteFile());
                }
            }
        }
        return all;
    }

    /**
     * 从文件中读取行到字符串列表
     *
     * @param filename
     * @return
     * @throws IOException
     */
    public static List<String> readLines(String filename) throws IOException {
        return Files.readAllLines(new File(filename).toPath());
    }

    /**
     * 压缩文件
     *
     * @param srcFilename
     * @param zipFilename
     * @throws IOException
     */
    public static void zipFile(String srcFilename, String zipFilename) throws IOException {
        File srcFile = new File(srcFilename);
        try (
                FileOutputStream fileOut = new FileOutputStream(zipFilename);
                ZipOutputStream zipOut = new ZipOutputStream(fileOut);
                FileInputStream fileIn = new FileInputStream(srcFile);
        ) {
            ZipEntry zipEntry = new ZipEntry(srcFile.getName());
            zipOut.putNextEntry(zipEntry);
            final byte[] bytes = new byte[1024];
            int length;
            while ((length = fileIn.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
        }
    }

    /**
     * 压缩多个文件
     *
     * @param srcFilenames
     * @param zipFilename
     * @throws IOException
     */
    public static void zipFiles(String[] srcFilenames, String zipFilename) throws IOException {
        try (
                FileOutputStream fileOut = new FileOutputStream(zipFilename);
                ZipOutputStream zipOut = new ZipOutputStream(fileOut);
        ) {
            for (int i = 0; i < srcFilenames.length; i++) {
                File srcFile = new File(srcFilenames[i]);
                try (FileInputStream fileIn = new FileInputStream(srcFile)) {
                    ZipEntry zipEntry = new ZipEntry(srcFile.getName());
                    zipOut.putNextEntry(zipEntry);
                    final byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fileIn.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }
                }
            }
        }
    }

    /**
     * 压缩目录
     *
     * @param srcDirectoryName
     * @param zipFileName
     * @throws IOException
     * @De
     */
    public static void zipDirectory(String srcDirectoryName, String zipFileName) throws IOException {
        File srcDirectory = new File(srcDirectoryName);
        try (
                FileOutputStream fileOut = new FileOutputStream(zipFileName);
                ZipOutputStream zipOut = new ZipOutputStream(fileOut)
        ) {
            zipFile(srcDirectory, srcDirectory.getName(), zipOut);
        }
    }

    /**
     * 压缩目录
     *
     * @param fileToZip
     * @param fileName
     * @param zipOut
     * @throws IOException
     */
    public static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)
            throws IOException {
        // Ignore hidden files as standard
        if (fileToZip.isHidden()) {
            return;
        }
        if (fileToZip.isDirectory()) {
            if (fileName.endsWith("/")) {
                // To be zipped next
                zipOut.putNextEntry(new ZipEntry(fileName));
                zipOut.closeEntry();
            } else {
                // Add the "/" mark explicitly to preserve structure while unzipping action is performed
                zipOut.putNextEntry(new ZipEntry(fileName + "/"));
                zipOut.closeEntry();
            }
            File[] children = fileToZip.listFiles();
            // Recursively apply function to all children
            for (File childFile : children) {
                zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
            }
            return;
        }
        try (
                // Start zipping once we know it is a file
                FileInputStream fis = new FileInputStream(fileToZip)
        ) {
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOut.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
        }
    }
}
